package common;

public class Product {
    /**
     * 商品编号
     */
    private int id;
    
    /**
     * 商品名称
     */
    private String name;
    
    /**
     * 数量
     */
    private int quantity;
    
    /**
     * 单价
     */
    private double unitPrice;
    
    /**
     * 总价
     */
    private double totalPrice;

    public Product() {
    }

    public Product(int id, String name, int quantity, double unitPrice) {
        this.id = id;
        this.name = name;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
        this.totalPrice = calculateTotalPrice();
    }
    
    /**
     * 计算总价
     */
    private double calculateTotalPrice() {
        return quantity * unitPrice;
    }
    
    /**
     * 当数量或单价变化时，重新计算总价
     */
    private void updateTotalPrice() {
        this.totalPrice = calculateTotalPrice();
    }

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
        updateTotalPrice();
    }

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
        updateTotalPrice();
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    @Override
    public String toString() {
        return "common.Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", quantity=" + quantity +
                ", unitPrice=" + unitPrice +
                ", totalPrice=" + totalPrice +
                '}';
    }
}